package StreamAPI;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {
    private static List<Person> javaProgrammers = new ArrayList<Person>() {
        {
            add(new Person("Elsdon", "Jaycob", "Java programmer", Gender.MALE.getLabel(), 43, 2000));
            add(new Person("Tamsen", "Brittany", "Java programmer", Gender.FEMALE.getLabel(), 23, 1500));
            add(new Person("Floyd", "Donny", "Java programmer", Gender.MALE.getLabel(), 33, 1800));
            add(new Person("Sindy", "Jonie", "Java programmer", Gender.FEMALE.getLabel(), 32, 1600));
            add(new Person("Vere", "Hervey", "Java programmer", Gender.MALE.getLabel(), 22, 1200));
            add(new Person("Maude", "Jaimie", "Java programmer", Gender.FEMALE.getLabel(), 27, 1900));
            add(new Person("Shawn", "Randall", "Java programmer", Gender.MALE.getLabel(), 30, 2300));
            add(new Person("Jayden", "Corrina", "Java programmer", Gender.FEMALE.getLabel(), 35, 1700));
            add(new Person("Palmer", "Dene", "Java programmer", Gender.MALE.getLabel(), 33, 2000));
            add(new Person("Addison", "Pam", "Java programmer", Gender.FEMALE.getLabel(), 34, 1300));
            add(new Person("Jarrod", "Pace", "PHP programmer", Gender.MALE.getLabel(), 34, 1550));
            add(new Person("Clarette", "Cicely", "PHP programmer", Gender.FEMALE.getLabel(), 23, 1200));
            add(new Person("Victor", "Channing", "PHP programmer", Gender.MALE.getLabel(), 32, 1600));
            add(new Person("Tori", "Sheryl", "PHP programmer", Gender.FEMALE.getLabel(), 21, 1000));
            add(new Person("Osborne", "Shad", "PHP programmer", Gender.MALE.getLabel(), 32, 1100));
            add(new Person("Rosalind", "Layla", "PHP programmer", Gender.FEMALE.getLabel(), 25, 1300));
            add(new Person("Fraser", "Hewie", "PHP programmer", Gender.MALE.getLabel(), 36, 1100));
            add(new Person("Quinn", "Tamara", "PHP programmer", Gender.FEMALE.getLabel(), 21, 1000));
            add(new Person("Alvin", "Lance", "PHP programmer", Gender.MALE.getLabel(), 38, 1600));
            add(new Person("Evonne", "Shari", "PHP programmer", Gender.FEMALE.getLabel(), 40, 1800));
        }
    };

    public static void main(String[] args) {
//        forEach(javaProgrammers);
//        filter(javaProgrammers);
//        limit(javaProgrammers);
//        paging(javaProgrammers);
//       min(javaProgrammers);
    }

    //遍历
    private static void forEach(List<Person> list){
        /*Stream<Person> stream=list.stream();
        stream.forEach(person -> System.out.println(person));*/

        list.stream()
                .forEach(person -> {
                    int salary= (int) (person.getSalary()*1.05);
                    person.setSalary(salary);
                });
        list.stream().forEach(System.out::println);
    }

    //过滤筛选
    private static void filter(List<Person> list){
        list.stream()
                .filter(person -> person.getSalary()>1400)
                .filter(person -> person.getGender().equals(Gender.FEMALE.getLabel()))
                .filter(person -> person.getJob().equals("PHP programmer"))
                .forEach(System.out::println);
    }

    //筛选个数
    private static void limit(List<Person> list){
        list.stream()
                .filter(person -> person.getSalary()>1500)
                .limit(5)
                .forEach(System.out::println);
    }

    //排序后分页
    private static void paging(List<Person> list){
        List<Person> java_programmer = list.stream()
                .filter(person -> person.getJob().equals("Java programmer"))
                .sorted(Comparator.comparingInt(Person::getSalary))   //根据工资排序
                .skip(5)  //排除前5条
                .limit(5) //取5条
                .collect(Collectors.toList());

        java_programmer.stream().forEach(System.out::println);

    }

    //求集合中工资最低的员工信息
    private static void min(List<Person> list){
        Optional<Person> min = list.stream()
                .min(Comparator.comparingInt(Person::getSalary));
        Person person = min.orElse(null);//存在Person则返回，反则替换null
        System.out.println(person);
    }

    //求集合中工资最高的员工信息
    private static void max(List<Person> list){
        Optional<Person> max = list.stream()
                .max(Comparator.comparingInt(Person::getSalary));
        Person person = max.orElse(null);
        System.out.println(person);
    }

    //将Person的Stream流转换为String的流
    private static void map(List<Person> list){
        String collect = list.stream()
                .map(person -> person.getFirstName() + person.getLastName())//将集合中的Person对象转换为其他的类型
                .collect(Collectors.joining("*"));
        System.out.println(collect);

        int sum = list.stream()
                .mapToInt(Person::getSalary)//将Person流转换为IntStream
                .sum();//聚合操作：  count,max,min,average,sum
        System.out.println(sum);
    }
}
